<?php

class TaskHelper extends Helper {

    private $_field;
    private $rule;

    public function __construct() {
        $this->_field = array('task_id', 'task_type', 'task_releid', 'task_info', 'task_status', 'task_addtime', 'task_dotime');
        $this->rule = array(
            'task_type' => array('require', '类型不能为空')
        );
        
    }
    /**
     * 任务保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function taskSave($data, $where = '') {
        if(is_numeric($where)){
            $where = array('`task_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $validator = Validator::checkRule($data, $this->rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        if (empty($where)) {
            $id = $model->save('task', $data);
            if ($id) {
                return array('success' => true, 'msg' => $id);
            } else {
                return array('success' => false, 'msg' => '保存失败');
            }
        } else {
            $model->update('task', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 所有记录
     * @param type $where
     * @return type
     */
    public function taskAll($where){
        if(is_numeric($where)){
            $where = array('`task_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $data = $model->getList('task', $this->_field, $where);
        return $data;
    }
    /**
     * 执行日志
     * @param type $data
     * @param type $where
     * @return type
     */
    public function logSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`tl_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        if (empty($where)) {
            $id = $model->save('task_log', $data);
            if ($id) {
                return array('success' => true, 'msg' => $id);
            } else {
                return array('success' => false, 'msg' => '保存失败');
            }
        } else {
            $model->update('task_log', $data, $where);
            return array('success' => true);
        }
    }
    
}   